'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');

class GroupService extends Service {
    async getList() {
        const {
            ctx,
            app
        } = this;
        // const {
        //     list
        // } = Mock.mock({
        //     "list|10": [{
        //         "url|1": [10002202, 20001000], //图片
        //         "building_name|1": ["龙华尚城", "水晶花园", "龚华新村", "城市青年公寓"], //楼盘名称
        //         "building_address|1": ["北京市昌平区沙河镇顺沙路19号"],
        //         name: "@cname", //发起人
        //         create_time: Date.now(),
        //         group_state:0,//团购状态
        //         state: 0, //状态
        //         area:"北京市市辖区昌平区",//区域
        //     }]
        // });
        // list.forEach((item) => {
        //     app.mysql.insert('groups', {
        //         ...item
        //     })
        // })
        // return '123'
        const {
            pageSize,
            pageIndex,
            building_name,
            building_address,
            state,
            group_state,
            area
        } = ctx.query;
        let sql = `select * from groups where 1=1 `;
        let sqlCount = `select count(id) as total from groups where 1=1 `;
        if (building_name) {
            sql += `and building_name like '%${building_name}%' `
            sqlCount += `and building_name like '%${building_name}%' `
        }
        if (building_address) {
            sql += `and building_address like '%${building_address}%' `
            sqlCount += `and building_address like '%${building_address}%' `
        }
        if (state) {
            sql += `and state = '${state}' `
            sqlCount += `and state = '${state}' `
        }
        if (group_state) {
            sql += `and group_state = '${group_state}' `
            sqlCount += `and group_state = '${group_state}' `
        }
        if(area) {
            sql += `and area like '%${area}%' `
            sqlCount += `and area like '%${area}%' `
        }
        //分页
        if (pageSize && pageIndex) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        let res = await app.mysql.query(sql);
        let [{
            total
        }] = await app.mysql.query(sqlCount);
        res.forEach((item, index) => {
            // 序号
            item.number = (pageIndex - 1) * 5 + index + 1;
        })
        return {
            data: res,
            total: total
        }
    }
    // 修改团购的状态
    async groupChange() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            state,
        } = ctx.request.body;
        let row = {
            id
        };
        if (state !== undefined) {
            row.state = state
        }
        let res = await app.mysql.update('groups', row);
        return res
    }
    // 删除团购
    async delGroup() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        let res = await app.mysql.delete('groups', {
            id
        });
        return res
    }
}

module.exports = GroupService;